package scene;

import geometrie.Point;
import geometrie.Rebouchage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Tetrahedron implements Modele3D {
	private List<Facette3D> facettes;

	public Iterator<Facette3D> getFacettes() {
		return facettes.listIterator();
	}
	
	public void addTriangulation(Point[] polygon) {
		facettes.addAll(Rebouchage.rebouchageTrouRecCache(polygon));
	}

	// TODO complete here!!

	/**
	 * Constructor for a new Tetrahedron
	 * 
	 * @param height
	 *            the height
	 * @param start
	 *            <to be completed>
	 */
	public Tetrahedron(double height, Point start) {
		facettes = new ArrayList<Facette3D>(4);
		double edge = (height * 3) / Math.sqrt(6);
		Point[] vertexArray = new Point[4];
		vertexArray[0] = start;
		Point newPoint;
		newPoint = new Point(start.getXCartesien(), start.getYCartesien(),
				start.getZCartesien());
		newPoint = newPoint.add(edge, 0, 0);
		vertexArray[1] = newPoint;
		newPoint = new Point(start.getXCartesien(), start.getYCartesien(),
				start.getZCartesien());
		newPoint = newPoint.add(edge / 2, 0, -edge * Math.sin(Math.PI / 3));
		vertexArray[2] = newPoint;
		newPoint = new Point(start.getXCartesien(), start.getYCartesien(),
				start.getZCartesien());
		double xg, yg, zg;
		xg = (vertexArray[0].getXCartesien() + vertexArray[1].getXCartesien() + vertexArray[2]
				.getXCartesien()) / 3;
		yg = (vertexArray[0].getYCartesien() + vertexArray[1].getYCartesien() + vertexArray[2]
				.getYCartesien()) / 3;
		zg = (vertexArray[0].getZCartesien() + vertexArray[1].getZCartesien() + vertexArray[2]
				.getZCartesien()) / 3;
		newPoint = new Point(xg, yg, zg);
		newPoint = newPoint.add(0, -height, 0);
		vertexArray[3] = newPoint;

		facettes.add(new Facette3D(vertexArray[0], vertexArray[1],
				vertexArray[2]));
		facettes.add(new Facette3D(vertexArray[0], vertexArray[1],
				vertexArray[3]));
		facettes.add(new Facette3D(vertexArray[0], vertexArray[2],
				vertexArray[3]));
		facettes.add(new Facette3D(vertexArray[1], vertexArray[2],
				vertexArray[3]));

	}

	@Override
	public String toString() {
		return "Tetrahedron [facettes=" + facettes + "]";
	}

	/*
	 * Main for unitary tests. public static void main(String [] args) {
	 * Modele3D pyramid = new Tetrahedron(10, new Point(0,0,0));
	 * System.out.println(pyramid); pyramid = new Tetrahedron(20, new
	 * Point(1,1,1)); System.out.println(pyramid); }
	 */
}
